package com.qf.mybatisplus.test.pachong;

import cn.hutool.core.util.RuntimeUtil;
import cn.hutool.json.JSONException;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.qf.mybatisplus.utils.ApplicationContextHelper;
import net.lightbody.bmp.BrowserMobProxy;
import net.lightbody.bmp.BrowserMobProxyServer;
import net.lightbody.bmp.client.ClientUtil;
import net.lightbody.bmp.core.har.*;
import net.lightbody.bmp.proxy.CaptureType;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.logging.LogType;
import org.openqa.selenium.logging.LoggingPreferences;
import org.openqa.selenium.remote.CapabilityType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;

public class DyGetData {
    private static BrowserMobProxy proxy;
    private static WebDriver driver;
    private static final Logger log = LoggerFactory.getLogger(DyGetData.class);

    /**
     * 初始化代理对象和浏览器对象
     */
    private static void browserProxy() {

        // 生产环境chromedriver.exe位置
        String chromedriver = "/usr/bin/chromedriver";
        boolean prod = ApplicationContextHelper.isProduct();
        if (!prod) {
            // 测试环境chromedriver.exe位置
            chromedriver = "D:\\soft\\chromedriver_win32\\chromedriver.exe";
        }
        // path: chromedirver.exe文件路径
        File file = new File(chromedriver);
        // 新建service 方便后面关闭chromedriver
        ChromeDriverService service = new ChromeDriverService.Builder().usingDriverExecutable(file).usingAnyFreePort().build();
//        System.setProperty("webdriver.chrome.driver", chromedriver);
        try {
            // 开启服务
            service.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
        ChromeOptions options = new ChromeOptions();
        // 禁用阻止弹出窗口
        options.addArguments("--disable-popup-blocking");
        // 启动无沙盒模式运行
        options.addArguments("no-sandbox");
        // 禁用扩展
        options.addArguments("disable-extensions");
        // 默认浏览器检查
        options.addArguments("no-default-browser-check");
        // 忽略证书检查
        options.addArguments("--ignore-certificate-errors");
//        options.addArguments("--user-data-dir=somedirectory");
        // 非测试环境隐藏浏览器
        if (prod) {
            options.addArguments("--disable-dev-shm-usage");
            // 设置隐藏浏览器
            options.addArguments("--headless");
        }

        Map<String, Object> prefs = new HashMap();
        prefs.put("credentials_enable_service", false);
        prefs.put("profile.password_manager_enabled", false);
        // 禁用保存密码提示框
        options.setExperimentalOption("prefs", prefs);
        LoggingPreferences logPrefs = new LoggingPreferences();
        logPrefs.enable(LogType.PERFORMANCE, Level.ALL);
        options.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
        proxy = new BrowserMobProxyServer();
        //设置监听端口，设置之后当前线程的所有请求都会被监听
        proxy.start(4566);

        Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);
        options.setCapability(CapabilityType.PROXY, seleniumProxy);

        driver = new ChromeDriver(service, options);
//        driver = new ChromeDriver(options);
        // 最大化浏览器
        driver.manage().window().maximize();
        // 等待5秒
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        proxy.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT);
        proxy.enableHarCaptureTypes(CaptureType.getCookieCaptureTypes());
    }

    public JSONObject getData(String cookieStr, String authorId , String chromePath) throws Exception {

        JSONObject resJson = null;
        try {
            //初始化代理对象和浏览器对象
            browserProxy();
            //在请求之前随便给代理监听命名，一定要在请求之前！！！
            proxy.newHar("test_har");
            //--------------------这里可以写自己在浏览器中的操作逻辑--------------------

            log.info("1、进入登录页");
            driver.navigate().to("https://www.xingtu.cn/?redirect_uri=/ad/creator/market");
            driver.manage().deleteAllCookies();
            Thread.sleep(2000);
            Cookie cookie = null;
            String[] b = cookieStr.split("; ");
            for (int i = 0; i < b.length; i++) {
                String[] c = b[i].split("=");
                cookie = new Cookie(c[0], c[1]);
                driver.manage().addCookie(cookie);
            }
            driver.navigate().refresh();
            log.info("2、添加cookie进入首页");
            driver.get("https://www.xingtu.cn/ad/creator/market#/");
            Thread.sleep(4000);
            //首先得到最先的窗口 权柄
            String FirstHandle = driver.getWindowHandle();
            // 找到输入框输入达人id
            WebElement input = driver.findElement(By.cssSelector(".el-input__inner"));
            //点击
            input.click();
            //相当于ctrl+a 快捷键全选
            input.sendKeys(Keys.chord(Keys.CONTROL, "a"));
            //快捷键删除
            input.sendKeys(Keys.DELETE);
            log.info("3、首页输入框输入达人id：" + authorId);
            input.sendKeys(authorId);
            Thread.sleep(4000);
            By xpath3 = By.xpath("/html/body/div[1]/div/div[3]/div/div[1]/div/div[1]/div[3]/div/div[2]");
            if (isExistElement(driver, xpath3)) {
                String tagName = driver.findElement(xpath3).getTagName();
                // 点击搜索按钮
                log.info("5、点击搜索按钮");
                driver.findElement(xpath3).click();
            }

            Thread.sleep(2000);
            log.info("6、滚动鼠标滚轮到页面最底部");
            WebElement webElement = driver.findElement(By.cssSelector("body"));
            webElement.click();
            webElement.sendKeys(Keys.END);
            Thread.sleep(2000);

            By xpath = By.xpath("/html/body/div[1]/div/div[3]/div/div[1]/div/div[3]/div/main/div[1]/div/div[2]/div[1]/div[1]");
            if (isExistElement(driver, xpath)) {
                log.info("7、点击进入主页");
                driver.findElement(xpath).click();
            }
            //判断窗口
            Set<String> windowHandles = driver.getWindowHandles();
            for (String winHandle : driver.getWindowHandles()) {
                //得到浏览器所有窗口的权柄为Set集合，遍历
                if (winHandle.equals(FirstHandle)) {
                    //如果为 之前的窗口 权柄跳出
                    continue;
                }
                //如果不为 最先的窗口 权柄，将 新窗口的操作权柄  给 driver
                driver.switchTo().window(winHandle);
                break;
            }

            Thread.sleep(6000);
            // 内容表现
            By xpath1 = By.xpath("/html/body/div[1]/div/div[3]/div[1]/div[1]/div[1]/div/section/div[1]/div[1]/div[1]/div/div[1]/div/div/div/div[3]");
            if (isExistElement(driver, xpath1)) {
                String tagName = driver.findElement(xpath1).getTagName();
                String text = driver.findElement(xpath1).getText();
                log.info("7、点击内容表现");
                driver.findElement(xpath1).click();
            }
            Thread.sleep(4000);
            // 连接用户
            By xpath2 = By.xpath("/html/body/div/div/div[3]/div/div[1]/div/div/section/div[1]/div[1]/div[1]/div/div[1]/div/div/div/div[4]");
            if (isExistElement(driver, xpath2)) {
                String tagName = driver.findElement(xpath2).getTagName();
                String text = driver.findElement(xpath2).getText();
                log.info("8、点击连接用户");
                driver.findElement(xpath2).click();
            }
            Thread.sleep(3000);
            //获取请求和响应信息
            Har har = proxy.getHar();
            //获取监听日志
            List<HarEntry> list = har.getLog().getEntries();
            resJson = new JSONObject();

            for (HarEntry harEntry : list) {
                //获取请求对象
                HarRequest harRequest = harEntry.getRequest();
                //获取请求Cookie
                List<HarCookie> harCookies = harRequest.getCookies();
//                String requestBody = harRequest.getContent().getText();//获取请求参数
//                System.err.println("请求地址：" + harRequest.getUrl());
                String url = harRequest.getUrl();
                if (url.contains("get_author_spread_info")) {
                    driver.get(url);
                    String pageSource = driver.getPageSource();
                    String substring = pageSource.substring(pageSource.indexOf("{"), pageSource.lastIndexOf("}") + 1);
                    // 拿到数据
                    JSONObject jsonObject = JSONUtil.parseObj(substring);
                    log.info("内容表现接口数据：{}", jsonObject.toString());
                    resJson.put("showJson", jsonObject);
                }
                if (url.contains("author_audience_distribution")) {
                    driver.get(url);
                    String pageSource = driver.getPageSource();
                    String substring = pageSource.substring(pageSource.indexOf("{"), pageSource.lastIndexOf("}") + 1);
                    // 拿到数据
                    JSONObject jsonObject = JSONUtil.parseObj(substring);
                    log.info("连接用户接口数据：{}", jsonObject.toString());
                    resJson.put("connectJson", jsonObject);
                }
                //            获取响应对象
                //            HarResponse harResponse=harEntry.getResponse();
                //            System.err.println("响应结果:"+harResponse.toString());
                //            String redirectURL = harResponse.getRedirectURL();
                //            System.err.println("redirectURL地址:"+redirectURL);
                //
                //            List<HarCookie> harCookies2 = harResponse.getCookies();//获取响应Cookie
                //            String responseBody = harResponse.getContent().getText();//获取接口返回数据
                //            System.err.println("响应数据:"+responseBody);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        } finally {
            //自己想要的业务操作完了之后就可以把浏览器关了
            tearDown(chromePath);
        }

        return resJson;
    }


    //结束监听，关闭浏览器
    private static void tearDown( String chromePath) {
        if (proxy.isStarted()) {
            proxy.stop();
        }
        driver.quit();
        String result = RuntimeUtil.execForStr(chromePath);
        log.info("谷歌浏览器执行脚本结果 "+result);
    }

    public static boolean isExistElement(WebDriver webDriver, By by) {
        try {
            webDriver.findElement(by);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public static void main(String[] args) throws InterruptedException {

        String cookieStr = "Hm_lvt_5d77c979053345c4bd8db63329f818ec=1681977176; Hm_lpvt_5d77c979053345c4bd8db63329f818ec=1681977176; ttcid=6627b1e6b1034b80a8653dd64f34912930; s_v_web_id=verify_lgotsteu_doEardj2_sMBO_4ncU_8QaR_UfchC7gr1p3w; passport_csrf_token=e95cbdbbebfc4bda585c7ba02a6d2ab0; passport_csrf_token_default=e95cbdbbebfc4bda585c7ba02a6d2ab0; sid_guard=1e8be45510b36cb0695997e7ebd36a8d%7C1681977211%7C5184002%7CMon%2C+19-Jun-2023+07%3A53%3A33+GMT; uid_tt=98706b5678bfe283eaf6f5e81a635e25; uid_tt_ss=98706b5678bfe283eaf6f5e81a635e25; sid_tt=1e8be45510b36cb0695997e7ebd36a8d; sessionid=1e8be45510b36cb0695997e7ebd36a8d; sessionid_ss=1e8be45510b36cb0695997e7ebd36a8d; sid_ucp_v1=1.0.0-KDg4NjQzZDVmNDM4NzVjZWUxNzE4OTY5ODc4NzFlZDBmNmJkMzI4MDEKFwjYndCmzoyXBhD73oOiBhj6EzgBQOsHGgJobCIgMWU4YmU0NTUxMGIzNmNiMDY5NTk5N2U3ZWJkMzZhOGQ; ssid_ucp_v1=1.0.0-KDg4NjQzZDVmNDM4NzVjZWUxNzE4OTY5ODc4NzFlZDBmNmJkMzI4MDEKFwjYndCmzoyXBhD73oOiBhj6EzgBQOsHGgJobCIgMWU4YmU0NTUxMGIzNmNiMDY5NTk5N2U3ZWJkMzZhOGQ; x-jupiter-uuid=1681977211348166; csrf_session_id=49da9c56ceb0ddb83fe827105a91a783; star_sessionid=1e8be45510b36cb0695997e7ebd36a8d; tt_webid=7224037033332786700; csrftoken=Um00JbTs8azHx9ZFnAe1HZxnh0PMMpRT; msToken=a_zKxMD-wi1bWflUd3nqTlLZF2kjIxCbGQz1gRcKcHIfJd-bk7YvbZw-znM4Ukrhm2Tz9vkuCK369qps2k_80qK95hje7tYZlHkk9FUfAz9GvXjN2XTb; tt_scid=p-9hn6lHbQMDOse92.ZyddJ.B6HhVKcUcJXJmifCdh7ii0EX6qXSWZpnqcVi98MJ5cac";
        String authorId = "6868799121650614276";


        JSONObject resJson = null;
        try {

            // 生产环境chromedriver.exe位置
            String chromedriver = "/usr/bin/chromedriver";
            boolean prod = false;
            if (!prod) {
                // 测试环境chromedriver.exe位置
                chromedriver = "/usr/local/bin/chromedriver";
            }
            // path: chromedirver.exe文件路径
            File file = new File(chromedriver);
            // 新建service 方便后面关闭chromedriver
            ChromeDriverService service = new ChromeDriverService.Builder().usingDriverExecutable(file).usingAnyFreePort().build();
//        System.setProperty("webdriver.chrome.driver", chromedriver);
            try {
                // 开启服务
                service.start();
            } catch (IOException e) {
                e.printStackTrace();
            }
            ChromeOptions options = new ChromeOptions();
            // 禁用阻止弹出窗口
            options.addArguments("--disable-popup-blocking");
            // 启动无沙盒模式运行
            options.addArguments("no-sandbox");
            // 禁用扩展
            options.addArguments("disable-extensions");
            // 默认浏览器检查
            options.addArguments("no-default-browser-check");
            // 忽略证书检查
//            options.addArguments("--ignore-certificate-errors");
//        options.addArguments("--user-data-dir=somedirectory");
            // 非测试环境隐藏浏览器
            if (prod) {
                options.addArguments("--disable-dev-shm-usage");
                // 设置隐藏浏览器
                options.addArguments("--headless");
            }

//            Map<String, Object> prefs = new HashMap();
//            prefs.put("credentials_enable_service", false);
//            prefs.put("profile.password_manager_enabled", false);
//            // 禁用保存密码提示框
//            options.setExperimentalOption("prefs", prefs);
//            LoggingPreferences logPrefs = new LoggingPreferences();
//            logPrefs.enable(LogType.PERFORMANCE, Level.ALL);
//            options.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
//            proxy = new BrowserMobProxyServer();
//            //设置监听端口，设置之后当前线程的所有请求都会被监听
//            proxy.start(4566);
//
//            Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);
//            options.setCapability(CapabilityType.PROXY, seleniumProxy);

            driver = new ChromeDriver(service, options);
//        driver = new ChromeDriver(options);
            // 最大化浏览器
            driver.manage().window().maximize();
            // 等待5秒
//            driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
//            proxy.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT);
//            proxy.enableHarCaptureTypes(CaptureType.getCookieCaptureTypes());


            //初始化代理对象和浏览器对象
//            browserProxy();
//            proxy.newHar("test_har");//在请求之前随便给代理监听命名，一定要在请求之前！！！
            //--------------------这里可以写自己在浏览器中的操作逻辑--------------------
//        driver.get("http://www.baidu.com");//随便发个请求

            log.info("1、进入登录页");
            driver.navigate().to("https://www.xingtu.cn/?redirect_uri=/ad/creator/market");

            driver.manage().deleteAllCookies();
            Thread.sleep(2000);
            Cookie cookie = null;
            String[] b = cookieStr.split("; ");
            for (int i = 0; i < b.length; i++) {
                String[] c = b[i].split("=");
                cookie = new Cookie(c[0], c[1]);
                driver.manage().addCookie(cookie);
            }
            driver.navigate().refresh();
            log.info("2、添加cookie进入首页");
            driver.get("https://www.xingtu.cn/ad/creator/market#/");
            Thread.sleep(4000);

            String FirstHandle = driver.getWindowHandle();     //首先得到最先的窗口 权柄
            // 找到输入框输入达人id
            WebElement input = driver.findElement(By.cssSelector(".el-input__inner"));
            input.click();//点击
            input.sendKeys(Keys.chord(Keys.CONTROL, "a"));//相当于ctrl+a 快捷键全选
            input.sendKeys(Keys.DELETE);//快捷键删除
            log.info("3、首页输入框输入达人id");
            input.sendKeys(authorId);
            Thread.sleep(2000);
            By xpath3 = By.xpath("/html/body/div[1]/div/div[3]/div/div[1]/div/div[1]/div[3]/div/div[2]");
            if (isExistElement(driver, xpath3)) {
                String tagName = driver.findElement(xpath3).getTagName();
                // 点击搜索按钮
                log.info("5、点击搜索按钮");
                driver.findElement(xpath3).click();
                //driver.findElement(By.cssSelector(".search-btn")).click();
            }

            Thread.sleep(2000);
            log.info("6、滚动鼠标滚轮到页面最底部");
            WebElement webElement = driver.findElement(By.cssSelector("body"));
            webElement.click();
            webElement.sendKeys(Keys.END);
            Thread.sleep(2000);

            By xpath = By.xpath("/html/body/div[1]/div/div[3]/div/div[1]/div/div[3]/div/main/div[1]/div/div[2]/div[1]/div[1]");
            if (isExistElement(driver, xpath)) {
                log.info("7、点击进入主页");
                driver.findElement(xpath).click();

            }
            //判断窗口
            Set<String> windowHandles = driver.getWindowHandles();
            for (String winHandle : driver.getWindowHandles()) {
                //得到浏览器所有窗口的权柄为Set集合，遍历
                if (winHandle.equals(FirstHandle)) {
                    //如果为 之前的窗口 权柄跳出
                    continue;
                }
                //如果不为 最先的窗口 权柄，将 新窗口的操作权柄  给 driver
                driver.switchTo().window(winHandle);
                break;
            }
            Thread.sleep(4000);
            // 内容表现
            By xpath1 = By.xpath("/html/body/div[1]/div/div[3]/div[1]/div[1]/div[1]/div/section/div[1]/div[1]/div[1]/div/div[1]/div/div/div/div[3]");
            if (isExistElement(driver, xpath1)) {
                String tagName = driver.findElement(xpath1).getTagName();
                String text = driver.findElement(xpath1).getText();
                log.info("7、点击内容表现");
                driver.findElement(xpath1).click();
            }
            Thread.sleep(4000);
//            // 连接用户
//            By xpath2 = By.xpath("/html/body/div/div/div[3]/div/div[1]/div/div/section/div[1]/div[1]/div[1]/div/div[1]/div/div/div/div[4]");
//            if (isExistElement(driver, xpath2)) {
//                String tagName = driver.findElement(xpath2).getTagName();
//                String text = driver.findElement(xpath2).getText();
//                log.info("8、点击连接用户");
//                driver.findElement(xpath2).click();
//            }


            log.info("8、滚动鼠标滚轮到页面最底部");
            WebElement webElement5 = driver.findElement(By.cssSelector("body"));
            webElement5.click();

            for (int i = 0;i<45;i++){
                webElement5.sendKeys(Keys.DOWN);
            }
            Thread.sleep(5000);

// /html/body/div[1]/div/div[3]/div/div[1]/div/div/section/div[2]/div/div[5]/div/div/div[3]/div/div[1]/div[1]/div/div/div[2]/img
// /html/body/div[1]/div/div[3]/div/div[1]/div/div/section/div[2]/div/div[5]/div/div/div[3]/div/div[2]/div[1]/div/div/div[2]/img

            By xpath5 = By.xpath("/html/body/div[1]/div/div[3]/div/div[1]/div/div/section/div[2]/div/div[6]/div/div/div[3]/div/div[1]/div[1]/div/div/div[2]/img");

            if (isExistElement(driver, xpath5)) {
                WebElement element = driver.findElement(xpath5);
                Actions action = new Actions(driver);
                log.info("9、鼠标移动到第一个视频上方");
                action.moveToElement(element).perform();
                Thread.sleep(1000);
            }

            By xpath6 = By.xpath("/html/body/div[1]/div/div[3]/div/div[1]/div/div/section/div[2]/div/div[6]/div/div/div[3]/div/div[2]/div[1]/div/div/div[2]/img");
            if (isExistElement(driver, xpath6)) {
                WebElement element = driver.findElement(xpath6);
                Actions action = new Actions(driver);
                log.info("10、鼠标移动到第二个视频上方");
                action.moveToElement(element).perform();
                Thread.sleep(1000);
            }
            Thread.sleep(3000);
            //获取请求和响应信息
            Har har = proxy.getHar();
            //获取监听日志
            List<HarEntry> list = har.getLog().getEntries();
            resJson = new JSONObject();

            for (HarEntry harEntry : list) {
                //获取请求对象
                HarRequest harRequest = harEntry.getRequest();
                //获取请求Cookie
                List<HarCookie> harCookies = harRequest.getCookies();
//                String requestBody = harRequest.getContent().getText();//获取请求参数
//                System.err.println("请求地址：" + harRequest.getUrl());
                String url = harRequest.getUrl();
                if (url.contains("get_author_spread_info")) {
                    driver.get(url);
                    String pageSource = driver.getPageSource();
                    String substring = pageSource.substring(pageSource.indexOf("{"), pageSource.lastIndexOf("}") + 1);
                    // 拿到数据
                    JSONObject jsonObject = JSONUtil.parseObj(substring);
                    log.info("内容表现接口数据：{}", jsonObject.toString());
                    resJson.put("showJson", jsonObject);
                }
                if (url.contains("author_audience_distribution")) {
                    driver.get(url);
                    String pageSource = driver.getPageSource();
                    String substring = pageSource.substring(pageSource.indexOf("{"), pageSource.lastIndexOf("}") + 1);
                    // 拿到数据
                    JSONObject jsonObject = JSONUtil.parseObj(substring);
                    log.info("连接用户接口数据：{}", jsonObject.toString());
                    resJson.put("connectJson", jsonObject);
                }

                if (url.contains("GetVideoInfo")&&url.contains("GetVideoInfo")) {
                    System.err.println("请求路径："+url);
                    driver.get(url);
                    String pageSource = driver.getPageSource();
                    String substring = pageSource.substring(pageSource.indexOf("{"), pageSource.lastIndexOf("}") + 1);
                    // 拿到数据
                    JSONObject jsonObject = JSONUtil.parseObj(substring);
                    log.info("获取视频链接接口数据：{}", jsonObject.toString());
                    resJson.put("videoJson", jsonObject);
                }

                //            获取响应对象
                //            HarResponse harResponse=harEntry.getResponse();
                //            System.err.println("响应结果:"+harResponse.toString());
                //            String redirectURL = harResponse.getRedirectURL();
                //            System.err.println("redirectURL地址:"+redirectURL);
                //
                //            List<HarCookie> harCookies2 = harResponse.getCookies();//获取响应Cookie
                //            String responseBody = harResponse.getContent().getText();//获取接口返回数据
                //            System.err.println("响应数据:"+responseBody);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        } finally {
//            if (proxy.isStarted()) {
//                proxy.stop();
//            }
            driver.quit();//自己想要的业务操作完了之后就可以把浏览器关了
        }

    }

}
